/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principale.java
 *
 * Created on 13 mai 2009, 14:13:08
 */
package vue;

import Modele.BaseDeDonnees;
import Modele.Constituer;
import Modele.Donnees;
import java.awt.Component;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

/**
 *
 * @author Mathieu PASSENAUD
 * version 1.0
 */
public class Principale extends javax.swing.JFrame {

    private DefaultMutableTreeNode racine;

    /** Creates new form Principale */
    public Principale() {
        BaseDeDonnees.connectionALaBase();
        initComponents();
        setTitle("Déduplication de données");
        this.actualiserAffichage();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        arbre = new javax.swing.JTree();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("RESTAURER");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Ajouter");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Supprimer");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jButton4.setText("Paramètres");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        arbre.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                arbreValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(arbre);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 597, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addGap(52, 52, 52)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 645, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jLabel1)
                    .addComponent(jButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser jf = new JFileChooser(".");
        jf.setMultiSelectionEnabled(true);
        jf.setFileSelectionMode(jf.FILES_AND_DIRECTORIES);
        this.getRootPane().add(jf);
        jf.showOpenDialog(null);
        File[] fichier = jf.getSelectedFiles();
        for (int i = 0; i < fichier.length; i++) {
            if (fichier[i].isFile()) {
                Modele.Main.enregistrerFichier(fichier[i], this);
            } else {
                Modele.Main.lister(fichier[i], this);
            }
        }
        Donnees.viderCache();
        Constituer.viderCache();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        DefaultMutableTreeNode leNoeud = (DefaultMutableTreeNode) arbre.getLastSelectedPathComponent();
        String s = leNoeud.getUserObject().toString();
        BaseDeDonnees.restaurer(new File(s));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //BaseDeDonnees.supprimer(jList1.getSelectedValue().toString());
        this.actualiserAffichage();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.dispose();
        new VueParametres().setVisible(true);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void arbreValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_arbreValueChanged
        DefaultMutableTreeNode leNoeud = (DefaultMutableTreeNode) arbre.getLastSelectedPathComponent();
        leNoeud.getUserObject().toString();

    }//GEN-LAST:event_arbreValueChanged
    private DefaultMutableTreeNode recupererSousNoeuds(String parent, DefaultMutableTreeNode racine) {
        ArrayList<String> liste = BaseDeDonnees.retournerRepertoires(parent);
        ArrayList<String> listeFichiers = BaseDeDonnees.retournerFichiers(parent);

        for (int i = 0; i < liste.size(); i++) {
            DefaultMutableTreeNode noeud = new DefaultMutableTreeNode(liste.get(i));
            racine.add(noeud);

            noeud = this.recupererSousNoeuds(liste.get(i), noeud);
        }
        for (int j = 0; j < listeFichiers.size(); j++) {
            DefaultMutableTreeNode noeud2 = new DefaultMutableTreeNode(listeFichiers.get(j));
            racine.add(noeud2);
        }
        return racine;
    }

    public void actualiserAffichage() {
        ArrayList<String> liste = BaseDeDonnees.retournerRepertoires("");
        ArrayList<String> liste2 = BaseDeDonnees.retournerFichiers("");
        if (liste == null) {
            this.dispose();
            new VueParametres().setVisible(true);
        }

        racine = new DefaultMutableTreeNode("Fichiers");
        for (int i = 0; i < liste.size(); i++) {
            DefaultMutableTreeNode noeud = new DefaultMutableTreeNode(liste.get(i));
            noeud = this.recupererSousNoeuds(liste.get(i), noeud);
            racine.add(noeud);
        }
        for (int i = 0; i < liste2.size(); i++) {
            DefaultMutableTreeNode noeud = new DefaultMutableTreeNode(liste2.get(i));
            racine.add(noeud);
        }
        arbre.setModel(new DefaultTreeModel(racine));
        Component c;
        int code = 0, cons = 0;

        ResultSet rs = BaseDeDonnees.executerRequete("select count(id_code) from code");
        try {
            rs.next();
        } catch (SQLException ex) {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            code = rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
        }
        rs = BaseDeDonnees.executerRequete("select count(id_code) from constituer");
        try {
            rs.next();
        } catch (SQLException ex) {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            cons = rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
        }
        int rapport;
        if (cons == 0) {
            rapport = 0;
        } else {
            rapport = (100 * code) / cons;
        }
        if (rapport > 100) {
            rapport = 0;
        }
        jLabel1.setText(rapport + "% de doublons actuellement");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbre;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
